pipeline {
    agent any
    // 环境变量
    environment {
        // docker私有仓库凭证
        harbor_docker_credentials = credentials('harbor-docker')
        // 镜像版本
        image_tag = sh(returnStdout: true, script: 'echo `date +"%Y%m%d%H%M"`').trim()
    }
    // 入参定义
    parameters {
        string(name: 'project_name', defaultValue: 'tz-hospital-ssm-server', description: '项目名称')
        string(name: 'git_url', defaultValue: 'http://admin@172.16.108.28:8901/r/tz-hospital-ssm-server.git', description: '仓库地址')
        string(name: 'branch_name', defaultValue: 'dockerSwarm', description: 'git分支')
        string(name: 'profiles', defaultValue: 'test', description: '环境')
        string(name: 'registry_url', defaultValue: '193.193.188.91', description: '镜像仓库地址')
        string(name: 'registry_ns', defaultValue: 'ssm-docker-swarm', description: '镜像命名空间')
    }
    stages {
        stage('检出代码') {
            steps {
                // 检出代码
                checkout([$class                           : 'GitSCM', branches: [[name: "*/${params.branch_name}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions                       : [],
                          submoduleCfg                     : [],
                          userRemoteConfigs                : [[
                                                                      credentialsId: 'gitblit',
                                                                      url          : "${params.git_url}"]]])
            }
        }
        stage("编译打包") {
            steps {
                //sh "echo ${env.WORKSPACE}"
                //  删除work目录
                sh "rm -f app.jar"
                //sh "pwd"
                // 编译打包
                sh "mvn -B -DskipTests clean package"
                // 将生成的jar包复制到当前工作空间下
                sh "cp target/app.jar ./app.jar"
                sh "ls"
            }
        }
        stage('构建镜像及推送到docker仓库') {
            steps {
                sh "pwd"
                // 服务器先执行 sudo chmod a+rw /var/run/docker.sock
                // 登录镜像仓库
                sh "docker login -u ${harbor_docker_credentials_USR} -p ${harbor_docker_credentials_PSW} ${params.registry_url}"
                // 构建镜像
                sh "docker build -t ${params.registry_url}/${params.registry_ns}/${params.project_name}:${image_tag} ."
                // 推送镜像到私服
                sh "docker push ${params.registry_url}/${params.registry_ns}/${params.project_name}:${image_tag}"
                // 删除当前目录下的app.jar
                sh "rm -rf app.jar"
                //delete docker images
                sh "docker rmi ${params.registry_url}/${params.registry_ns}/${params.project_name}:${image_tag}"
            }
        }
        stage('执行远程命令') {
            steps {
                script {
                    def imgName = "10.15.3.32:1801/${params.registry_ns}/${params.project_name}:${image_tag}"
                    def sshServer = getServer('193.193.188.121')
                    sh "echo " + imgName
                    // 更新或下载镜像
                    sshCommand remote: sshServer, command: "docker service update --image=${imgName} service01_app"
                }
            }
        }
    }

}

def getServer(ip) {
    def remote = [:]
    remote.name = "server-${ip}"
    remote.host = ip
    remote.port = 22
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: 'docker-swarm', passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote.user = "${userName}"
        remote.password = "${password}"
    }
    return remote
}